package com.company.cloud.pay.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.pay.modules.api.constant.PayTradeType;
import com.company.cloud.pay.modules.api.request.PayOrderCancelRequest;
import com.company.cloud.pay.modules.api.request.PayOrderCloseRequest;
import com.company.cloud.pay.modules.api.request.PayOrderQueryRequest;
import com.company.cloud.pay.modules.api.request.PayOrderRequest;
import com.company.cloud.pay.modules.api.request.RefundOrderQueryRequest;
import com.company.cloud.pay.modules.api.request.RefundOrderRequest;
import com.company.cloud.pay.modules.api.response.PayOrderCancelResponse;
import com.company.cloud.pay.modules.api.response.PayOrderCloseResponse;
import com.company.cloud.pay.modules.api.response.PayOrderQueryResponse;
import com.company.cloud.pay.modules.api.response.PayOrderResponse;
import com.company.cloud.pay.modules.api.response.RefundOrderResponse;
import com.company.cloud.pay.modules.api.service.PayRemoteService;
import com.company.cloud.pay.modules.pay.service.UnifiedPayService;
import com.company.cloud.pay.modules.pay.utils.PayTradeTypeUtil;
import com.company.common.server.config.cloud.ProjectConfig;

@RestController
@Service
public class PayRemoteServiceImpl implements PayRemoteService {

	@Autowired
	private UnifiedPayService unifiedPayService;
	
	@Autowired
	private ProjectConfig projectConfig;

	/** 统一下单 */
	@SuppressWarnings("unchecked")
	@Override
	public  BaseClientResult<PayOrderResponse> payOrderInfo(PayOrderRequest request) {
		try {
			ValidatorUtils.validate(request);
			PayTradeType ptt = PayTradeTypeUtil.getPayTradeTypeByCode(request.getPayTradeType());
			request.setPtt(ptt);
			PayOrderResponse data = unifiedPayService.payOrderInfo(request);
			return BaseClientResult.success(data);
		} catch (Exception e) {
			e.printStackTrace();
			return BaseClientResult.error(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public  BaseClientResult<PayOrderCancelResponse> payOrderCancel(PayOrderCancelRequest request) {
		try {
			ValidatorUtils.validate(request);
			PayTradeType ptt = PayTradeTypeUtil.getPayTradeTypeByCode(request.getPayTradeType());
			request.setPtt(ptt);
			return BaseClientResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return BaseClientResult.error(e);
		}
	
	}

	/** 关闭订单 */
	@SuppressWarnings("unchecked")
	@Override
	public  BaseClientResult<PayOrderCloseResponse> payOrderClose(PayOrderCloseRequest request) {
		try {
			ValidatorUtils.validate(request);
			PayTradeType ptt = PayTradeTypeUtil.getPayTradeTypeByCode(request.getPayTradeType());
			request.setPtt(ptt);
			PayOrderCloseResponse data = unifiedPayService.payOrderClose(request);
			return BaseClientResult.success(data);
		} catch (Exception e) {
			e.printStackTrace();
			return BaseClientResult.error(e);
		}
	}

	/** 查询订单 */
	@SuppressWarnings("unchecked")
	@Override
	public  BaseClientResult<PayOrderQueryResponse> payOrderQuery(PayOrderQueryRequest request) {
		try {
			ValidatorUtils.validate(request);
			PayTradeType ptt = PayTradeTypeUtil.getPayTradeTypeByCode(request.getPayTradeType());
			request.setPtt(ptt);
			PayOrderQueryResponse data= unifiedPayService.payOrderQuery(request);
			return BaseClientResult.success(data);
		} catch (Exception e) {
			e.printStackTrace();
			return BaseClientResult.error(e);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public  BaseClientResult<RefundOrderResponse> refundOrderInfo(RefundOrderRequest request) {
		try {
			ValidatorUtils.validate(request);
			PayTradeType ptt = PayTradeTypeUtil.getPayTradeTypeByCode(request.getPayTradeType());
			request.setPtt(ptt);
			return BaseClientResult.success();
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public  BaseClientResult<RefundOrderQueryRequest> refundOrderQuery(RefundOrderQueryRequest request) {
		try {
			ValidatorUtils.validate(request);
			PayTradeType ptt = PayTradeTypeUtil.getPayTradeTypeByCode(request.getPayTradeType());
			request.setPtt(ptt);
			return BaseClientResult.success();
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
		
	}

}
